package controllers.product;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib_model.Account.Account;
import lib_model.Factories.Factory;
import lib_model.Orther.ReturnObj;
import lib_model.Product.Products;

/**
 * Servlet implementation class ProductCreate
 */
@WebServlet("/product/ProductCreate")
public class ProductCreate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProductCreate() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Products p = new Products();
		p.tax_product_id_ref = 1;
		p.categories_id_ref = 1;
		Account r = ((Account)request.getSession().getAttribute("User"));
		p.username_ref = r.username;
		p.type = 1;
		int a = (Integer) p.Add().ObjResult;
		response.sendRedirect("../administrator/product-new.jsp?type=new&id="
				+ a);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int id = (Integer.parseInt(request.getParameter("id")));
			int cater = (Integer.parseInt(request.getParameter("txtCater")));
			String name = request.getParameter("txtName");
			float price = (Float.parseFloat(request.getParameter("txtPrice")));
			int tax = (Integer.parseInt(request.getParameter("txtTax")));
			String des = request.getParameter("txtDes");
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/product.jsp");
			if (name != null && request.getParameter("txtCater") != null
					&& request.getParameter("txtPrice") != null
					&& request.getParameter("txtTax") != null && des != null) {
				int sta = 1;
				if (request.getParameter("sta").equals("0"))
					sta = 0;
				Products p = new Products();
				p.name = name;
				p.description = des;
				p.categories_id_ref = cater;
				p.tax_product_id_ref = tax;
				p.username_ref = ((Account) request.getSession().getAttribute("User")).username;
				p.price = price;
				p.type = sta;
				p.id = id;
				ReturnObj obj = p.Update();
				if (obj.Result == true) {
					System.out.println(obj.Result);
					request.setAttribute("Mes", "Suscessfull !");
				} else {
					System.out.println(obj.Result);
					request.setAttribute("Mes",
							"Error ! Can't create product 1");
				}
			} else {
				request.setAttribute("Mes", "Error ! Can't create product 2");
			}
			dis.forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			RequestDispatcher dis = request
					.getRequestDispatcher("../administrator/product.jsp");
			request.setAttribute("Mes", "Error ! Can't create product 3");
			dis.forward(request, response);
		}
	}
}
